#ifndef utility_h
#define utility_h

#include <windows.h>		// for windows SendInput

class Utility
{

	private:
	
	public:
	
		#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
		static UINT keyToScanCode( UINT key )
		{
		
			return ( MapVirtualKey( key, MAPVK_VK_TO_VSC ) );
		
		}
		
		static void sendInput( std::string windowName, int key, int keyDelay )
		{
		
			// Find the window & set it to foreground before sending input
            HWND window = FindWindow( 0, windowName.c_str() );
			SetForegroundWindow( window );
		
			// Build the input structure
			INPUT input = {0};
			
			// Build the keyboard structure
			KEYBDINPUT keyboard = {0};	// initialize to all null
			
			// Assign the virtual key (word Virtual key)
			keyboard.wVk = 0;
			keyboard.wScan = keyToScanCode( key );
			keyboard.dwFlags = KEYEVENTF_SCANCODE;
			keyboard.time = 0;
			
			// Assign the input type and keyboard structure
			input.type = INPUT_KEYBOARD;
			input.ki = keyboard;
			
			// Send the key down
			SendInput( 1, &input, sizeof( input ) );
			Sleep( keyDelay );
			
			
			// Clear the memory for the structures
			std::memset( &keyboard, 0, sizeof( KEYBDINPUT ) );
			std::memset( &input, 0, sizeof( INPUT ) );
			
			// Assign the key to be up
			keyboard.wVk = 0;
			keyboard.wScan = keyToScanCode( key );
			keyboard.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_SCANCODE;
			keyboard.time = 0;
			
			// Assign the input type and keyboard structure (again, for key up)
			input.type = INPUT_KEYBOARD;
			input.ki = keyboard;
			
			// Send the key up
			SendInput( 1, &input, sizeof( input ) );
			Sleep( keyDelay );
		
		}
		#endif

};

#endif
